//
//  HJJRootHeadView.m
//  Pianke
//
//  Created by 黄俊杰 on 15/12/23.
//  Copyright © 2015年 dada. All rights reserved.
//

#import "HJJRootHeadView.h"
#import "HJJRadioRootModel.h"

@implementation HJJRootHeadView

-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

-(void)createView{

    //滚动屏
    self.picScroll = [[UIScrollView alloc] init];
    self.picScroll.contentSize = CGSizeMake(375 * 5 * EACH_WIDTH, 140 * EACH_HEIGHT);
    self.picScroll.backgroundColor = [UIColor grayColor];
    self.picScroll.alwaysBounceHorizontal = 1;
    self.picScroll.pagingEnabled = 1;
    [self.picScroll setContentOffset:CGPointMake(375 * EACH_HEIGHT, 0)];
    self.picScroll.delegate = self;
    self.picScroll.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.picScroll];
    
    [self.picScroll mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-110);
    }];
    
    for (int i = 0; i < 3; i++) {
        UIImageView *underImage = [[UIImageView alloc] init];
        underImage.backgroundColor = [UIColor grayColor];
        underImage.layer.cornerRadius = 10;
        underImage.tag = 1000 + i;
        underImage.userInteractionEnabled = 1;
        [self addSubview:underImage];
        
        [underImage mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo((10 + i * 122) * EACH_WIDTH);
            make.top.mas_equalTo(self.picScroll.mas_bottom).offset(10 * EACH_HEIGHT);
            make.size.mas_equalTo(CGSizeMake(111 * EACH_WIDTH, 100 * EACH_HEIGHT));
        }];
        
        HJJTapGestureRecognizer *tap = [[HJJTapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        tap.key = [NSString stringWithFormat:@"%d",i];
        [underImage addGestureRecognizer:tap];
    }
    
    //定时器
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(picChange:) userInfo:nil repeats:1];

}

-(void)tapAction:(HJJTapGestureRecognizer *)tap{

    
    [self.headViewDele goToNextView:tap.key];
}

-(void)sendPicArray:(NSMutableArray *)picArray{
    
    self.picScroll.contentSize = CGSizeMake(375 * (picArray.count + 2) *EACH_WIDTH, 140 * EACH_HEIGHT);
    self.picArray = [NSMutableArray arrayWithArray:picArray];
    
    NSMutableArray *imageArray = [NSMutableArray array];
    for (int i = 0; i < 3; i++) {
        HJJCarousel *carousel = [picArray objectAtIndex:i];
        [imageArray addObject:carousel.img];
    }
    
    UIImageView *firstImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375 * EACH_WIDTH, 140 * EACH_HEIGHT)];
    [firstImage sd_setImageWithURL:[imageArray lastObject] placeholderImage:nil];
    UIImageView *lastImage = [[UIImageView alloc] initWithFrame:CGRectMake(375 * (imageArray.count + 1) * EACH_WIDTH, 0, 375 * EACH_WIDTH, 140 * EACH_HEIGHT)];
    [lastImage sd_setImageWithURL:[imageArray objectAtIndex:0] placeholderImage:nil];
    [self.picScroll addSubview:firstImage];
    [self.picScroll addSubview:lastImage];
    for (int i = 0; i < imageArray.count; i++) {
        self.picImage = [[UIImageView alloc] initWithFrame:CGRectMake(375 * (i + 1) * EACH_WIDTH, 0, 375 * EACH_WIDTH, 140 * EACH_HEIGHT)];
        NSURL *picURL = [imageArray objectAtIndex:i];
        [self.picImage sd_setImageWithURL:picURL placeholderImage:nil];
        self.picImage.userInteractionEnabled = 1;
        [self.picScroll addSubview:self.picImage];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [self.picImage addGestureRecognizer:tap];
    }
}

-(void)sendUnderArray:(NSMutableArray *)underArray{

    for (int i = 0; i < 3; i++) {
        UIImageView *image = [self viewWithTag:i + 1000];
        HJJRadioRootModel *model = [underArray objectAtIndex:i];
        NSURL *url = [NSURL URLWithString:model.coverimg];
        [image sd_setImageWithURL:url placeholderImage:nil];
    }
}

-(void)picChange:(NSTimer *)timer
{
    
    [self.picScroll setContentOffset:CGPointMake(self.picScroll.contentOffset.x + 375 * EACH_WIDTH, 0)];
    [self scrollViewDidEndDecelerating:self.picScroll];
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    
    [self.timer invalidate];
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(picChange:) userInfo:nil repeats:1];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if (self.picScroll.contentOffset.x == 375 * (self.picArray.count + 1) * EACH_WIDTH) {
        [self.picScroll setContentOffset:CGPointMake(375 * EACH_WIDTH, 0)];
    }
    if (self.picScroll.contentOffset.x == 0) {
        [self.picScroll setContentOffset:CGPointMake(375 * self.picArray.count * EACH_WIDTH, 0)];
    }
    
}

@end
